<template>
  <div class="order container">
    <header>
      <i class="iconfont icon-fanhui" @click="$router.back()"></i>
      <span>提交订单</span>
      <i class="iconfont icon-kefu"></i>
    </header>
    <section>
      <div class="path">
        <h3 class="path-title">收货信息</h3>
        <div class="path-content" @click="goPath">
          <span>{{ path.name }}</span>
          <span>{{ path.tel }}</span>
          <div>
            <span>{{ path.province }}</span>
            <span>{{ path.city }}</span>
            <span>{{ path.county }}</span>
            <span>{{ path.addressDetail }}</span>
          </div>
        </div>
      </div>
      <div class="payment">
        <div class="payment-title">支付方式：</div>
        <van-radio-group v-model="radioPayment">
          <van-radio name="ali">支付宝支付</van-radio>
        </van-radio-group>
      </div>
      <div class="goods">
        <ul>
          <li v-for="item in goodsList" :key="item.id">
            <div>
              <img :src="item.imgUrl" alt="" />
            </div>
            <div class="goods-content">
              <h4>{{ item.name }}</h4>
              <div class="goods-total">
                <span class="price">¥{{ item.price }}</span>
                <span>x{{ item.num }}</span>
              </div>
            </div>
          </li>
        </ul>

        <hr />
        <div class="zongji">
          <p>合计：</p>
          <p class="price">￥{{ total.price }}</p>
        </div>
      </div>

      <div class="detail">
        <p class="p1">账单明细</p>
        <div class="border">
          <div class="zongjia">
            <p class="p2">订单总价</p>
            <p class="p3">￥ {{ total.price }}</p>
          </div>
          <div class="yunfei">
            <p class="p2">运费</p>
            <p class="p3">￥ {{ yunfei }}.00</p>
          </div>
          <div class="youhuiquan">
            <p class="p2">优惠券</p>
            <p class="p3">-￥ {{ youhuiquan.toFixed(2) }}</p>
          </div>
          <div class="shifukuan">
            <p class="p2">实付款</p>
            <p class="p3">
              ￥ {{ (total.price - -yunfei - youhuiquan).toFixed(2) }}
            </p>
          </div>
        </div>
      </div>
    </section>
    <footer>
      <div class="order-total">
        <span>共</span>
        <b>{{ total.num }}</b>
        <span>件,</span>
        <span>总金额：</span>
        <em>¥{{ yunfei - -total.price - youhuiquan }}</em>
      </div>
      <div class="order-topay" @click="goPayment">提交订单</div>
    </footer>
  </div>
</template>
<script>
import qs from "qs";
import http from "@/common/api/request.js";
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      radioPayment: "ali",
      path: [],
      yunfei: 5,
      youhuiquan: 0,
      total: {
        price: 0,
        num: 0,
      },
      id: [],
    };
  },
  computed: {
    ...mapState({
      orderId: (state) => state.order.orderId,
      orderList: (state) => state.order.orderList,
    }),
    orderList() {
      return this.$store.state.orderList;
    },
  },

  created() {
    // 选中的商品
    this.goodsList = this.$route.query.detail;
    this.selectOrder();
    this.orderLists();
    this.selectAddress();
  },

  methods: {
    ...mapMutations(["initOrder", "initList"]),
    // 获取订单列表
    orderLists() {
      this.initList(this.goodsList);
      this.goodsList.forEach((v) => {
        this.total.price += v.price * v.num;
        this.total.num += +v.num;
      });
    },
    //查询地址
    selectAddress() {
      http
        .$axios({
          url: "/api/selectAddress",
          method: "post",
          // headers:{
          //     token:true
          // }
        })
        .then((res) => {
          // this.initData( res.data );
          console.log(res.data);
          //有默认收货地址
          this.path = res.data[3];
          // if( this.defaultPath.length ){
          //     this.path = this.defaultPath[0];
          // }else{
          //     this.path = res.data[0];
          // }
        });
    },
    //选择收货地址
    goPath() {
      this.$router.push({
        path: "/path",
        query: {
          type: "select",
        },
      });
    },
    //提交订单
    goPayment() {
      //判断是否选择了收货地址
      if (!this.path) return Toast("请填写收货地址");
      http
        .$axios({
          url: "http://127.0.0.1:3008/goods/submitOrder",
          method: "post",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
          data: {
            orderId: this.orderId,
            goodsList: this.goodsList,
          },
        })
        .then((res) => {
          let newArr = [];
          this.goodsList.forEach((v) => {
            newArr.push(v.name);
          });
          // 支付传递的参数
          let dataOrder = {
            orderId: this.orderId,
            price: this.total.price,
            name: newArr.join(""),
          };
          if (res.success) {
            //去支付
            http
              .$axios({
                url: "http://127.0.0.1:3008/goods/payment",
                method: "post",
                headers: {
                  // 一定要配置这个请求头
                  "Content-Type": "application/x-www-form-urlencoded",
                },
                //qs是增加安全性的序列化
                data: qs.stringify(dataOrder),
              })
              .then((res) => {
                if (res.success) {
                  //打开支付宝支付的页面  paymentUrl需要打开的支付地址
                  window.location.href = res.paymentUrl;
                }
              });
          }
        });
    },

    //查询订单
    selectOrder() {
      http
        .$axios({
          url: "http://127.0.0.1:3008/goods/selectOrder",
          method: "post",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
          data: {
            orderId: this.orderId,
            goodsList: this.goodsList,
          },
        })
        .then((res) => {
          this.initOrder(res.data);
          this.total = {
            price: parseInt(res.data[0].price).toFixed(2),
            num: parseInt(res.data[0].num).toFixed(2),
          };
        });
    },
  },

  watch: {
    $route(to, from) {
      this.goodsList = [];
      this.selectOrder();
      this.orderLists();
      this.goodsList = this.$route.query.detail;
    },
  },
};
</script>

<style lang='scss' scoped>
.container {
  padding-right: 0px;
  padding-left: 0px;
  margin-right: auto;
  margin-left: auto;
  overflow: auto;
}
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 1.173333rem;
  color: #fff;
  background-color: #b0352f;
  i {
    padding: 0 0.4rem;
    font-size: 0.586666rem;
  }
  span {
    font-weight: 300;
    font-size: 0.48rem;
  }
}
section {
  overflow: auto;
  background-color: #f7f7f7;
  margin-bottom: 51px;
  padding-bottom: 151px;
  .path-title {
    padding: 0.4rem;
    font-size: 0.48rem;
  }
  .path-content {
    padding: 0.16rem 0.4rem;
    font-size: 0.373333rem;
    background-color: #ffffff;
    span {
      padding-right: 0.16rem;
    }
  }
  .payment {
    padding: 0.16rem 0.4rem;
    margin-top: 0.4rem;
    font-size: 0.426666rem;
    background-color: #ffffff;
    .van-radio-group {
      display: flex;
      padding: 0.16rem 0;
      .van-radio {
        padding-right: 0.266666rem;
      }
    }
  }
  .goods {
    padding: 0.16rem 0.4rem;
    margin-top: 0.4rem;
    font-size: 0.426666rem;
    background-color: #ffffff;
    ul {
      width: 100%;
      li {
        display: flex;
        width: 100%;
        margin-top: 40px;
        img {
          width: 1.973333rem;
          height: 1.973333rem;
        }
        .goods-content {
          flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          padding-left: 0.4rem;
          h4 {
            margin-top: -1px;
            font-size: 13px;
          }
          .goods-total {
            display: flex;
            justify-content: space-between;
            margin-top: 11px;
            .price {
              color: #b0352f;
            }
          }
        }
      }
    }

    .zongji {
      margin-top: 20px;
      font-size: 15px;
      display: flex;
      margin-left: 228px;

      .price {
        color: #b0352f;
        font-weight: bold;
      }
    }
  }

  .detail {
    margin-top: 20px;
    .p1 {
      font-size: 17px;
    }
    .border {
      background-color: #fff;
      font-size: 15px;
      padding-top: 20px;
      .zongjia,
      .yunfei,
      .youhuiquan,
      .shifukuan {
        display: flex;
        justify-content: space-between;
        .p2 {
          margin-left: 20px;
          color: #646464;
        }
        .p3 {
          margin-right: 25px;
          color: black;
          font-weight: bold;
        }
      }
    }
  }
}
footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 1.2rem;
  border-top: 1px solid #ccc;
  position: fixed;
  z-index: 2;
  bottom: 0;
  .order-total {
    font-size: 0.426666rem;
    span {
      padding: 0 0.16rem;
    }
    b {
      color: #b0352f;
    }
    em {
      font-size: 0.48rem;
      color: #b0352f;
    }
  }
  .order-topay {
    width: 3.2rem;
    line-height: 1.2rem;

    font-size: 0.426666rem;
    text-align: center;
    color: #fff;
    background-color: #1989fa;
    border-color: #1989fa;
  }
}
</style>
